# COPYRIGHT BLABLA

# README:
# Turn left/right (A,D) each press will change speed

from ogrebase import OgreView, EventListener
import ogre.io.OIS as OIS
import ogre.renderer.OGRE as ogre
import time
import math

class TestView(object):
    def __init__(self):
        self.OgreView = OgreView()
        self.InputHandler = \
                EventListener(self.OgreView.RenderWindow, True, True)
        self.QuitApplication = False

        self.InputHandler.register_key_event(OIS.KC_ESCAPE, self._quit)
        self.InputHandler.register_key_event(OIS.KC_A, self._left)
        self.InputHandler.register_key_event(OIS.KC_D, self._right)
        self.Angle = 0
        self.Turn = 0.01

    def __del__(self):
        if self.InputHandler:
            self.deinit()

    def deinit(self):
        del self.InputHandler
        del self.OgreView
        self.InputHandler = None

    def run(self):
        self._init_scene()

        while not self.QuitApplication:
            self.OgreView.render()
            self.InputHandler.update()
            self.set_cam_pos(self.Angle)
            time.sleep(0.01)
            self.Angle += self.Turn

    def _init_scene(self):
        self.OgreView.SceneManager.setAmbientLight(\
                ogre.ColourValue(0.7, 0.7, 0.7))
        self.OgreView.SceneManager.setSkyDome(True, 'Examples/CloudySky', 4,8)
        self.Light = self.OgreView.SceneManager.createLight('mainlight')
        self.Light.setPosition(ogre.Vector3(20, 80, 50))

        self.RootNode = self.OgreView.SceneManager.getRootSceneNode()

        self.Ogre = self.OgreView.SceneManager.createEntity('Ogre', \
                'ogrehead.mesh')
        self.NodeOgre = self.RootNode.createChildSceneNode('nodeOgre')
        self.NodeOgre.setPosition(ogre.Vector3(0,0,0))
        self.NodeOgre.attachObject(self.Ogre)

        self.set_cam_pos(self.Angle)

    def set_cam_pos(self, angle):
        self.OgreView.Camera.setPosition(ogre.Vector3(math.cos(angle)*200,\
                -10, math.sin(angle)*200))
        self.OgreView.Camera.lookAt(ogre.Vector3(0, 0, 1))
       

    def _quit(self, pressed):
        if not pressed:
            self.QuitApplication = True

    def _left(self, pressed):
        if pressed:
            self.Turn += 0.01

    def _right(self, pressed):
        if pressed:
            self.Turn -= 0.01

if __name__ == '__main__':
    try:
        t = TestView()
        t.run()
        t.deinit()
    except ogre.OgreException, e:
        print e

